import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // { path: '*', name: 'NotFound', component: () => import('@/views/notFound.vue') },
    { path: '/', component: () => import('@/views/myHome.vue') },
    { path: '/login', name: 'Login', component: () => import('@/views/userLogin.vue') },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/views/myHome.vue'),
      redirect: '/index',
      children: [
        { path: '/index', name: 'Index', component: () => import('@/views/myIndex.vue') },
        { path: '/user', name: 'User', component: () => import('@/views/myUser.vue') },
        {
          path: '/data-analysis',
          name: 'dataanalysis',
          component: () => import('@/views/myAnalysis.vue')
        }
      ]
    }
  ]
})

//路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      ElMessage({
        message: '登录过期,重新登录',
        type: 'warning'
      })
      next()
    } else {
      next('/login')
    }
  }
})

export default router
